<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class MDeleteController
extends MApplicationComponent
implements MIController
{
    private $_user_id = null;
    
    public function invoke($uri=null)
    {
                                parent::init();
        $params = $_REQUEST;
                if (isset($params) === false) {
            throw new MFileopsException(
                                        FXP::t('Bad Request'),
                                        MConst::HTTP_CODE_400);
        }
                        $user   = MUserManager::getInstance()->getCurrentUser();
        $device = MUserManager::getInstance()->getCurrentDevice();
        $this->_user_id             = $user->user_id;
        $user_nick                  = $user->user_name;
        $user_device_id           = $device->device_id;

                                $locale = "bytes";
        if (isset($params["locale"])) {
            $locale = $params["locale"];
        }
        if (isset($params["root"]) === false || isset($params["path"]) === false) {
            throw new MFileopsException(
                                        FXP::t('Bad Request'),
                                        MConst::HTTP_CODE_400);
        }
        $root = $params["root"];
        $path = $params["path"];
        
                                $path = MUtils::convertStandardPath($path);
        if ($path == "" || $path == "/" || $path === false)
        {
            throw new MFileopsException(
                        FXP::t('bad request'),
                                MConst::HTTP_CODE_400);
        }

                                $file_name   = MUtils::get_basename($path);
        $file_detail = new MFiles();
        $file_detail->file_name               = $file_name;
        $file_detail->file_path               = "/".$this->_user_id.$path;
                                $query_db_file = MFiles::queryFilesByPath($file_detail->file_path);
                if (count($query_db_file) <= 0)
        {
            throw new MFileopsException(
                        FXP::t('No file was not found at the specified path'),
                                MConst::HTTP_CODE_404);
        }

                                        $files                = array();
        $file_detail->is_dir  = false;
        if ($query_db_file[0]["file_type"] == MConst::OBJECT_TYPE_DIRECTORY)
        {
            $file_detail->is_dir  = true;
            $files = $this->handleChildrenFile($file_detail->file_path, $files);
        }
        else
        {
                        $this->handleFileMeta(
                                  $file_detail->file_path, 
                                  $query_db_file[0]["version_id"], 
                                  $this->_user_id, 
                                  $user_nick);
        }
                                $this->assembleFileDetail($file_detail, $query_db_file[0]);
        $ret_value = MFiles::updateRemoveFileDetail($file_detail);
        if ($ret_value === false)
        {
            throw new MFileopsException(
                                        FXP::t('Internal Server Error'),
                                        MConst::HTTP_CODE_500);
        }
        
                                array_push($files, $file_detail);
                                $ret_value = MEvents::batchCreateEvents($this->_user_id, $user_device_id, $files);
        if ($ret_value === false)
        {
            throw new MFileopsException(
                                        FXP::t('Internal Server Error'),
                                        MConst::HTTP_CODE_500);
        }
                $mime_type                  = "text/plain";
        $response                   = array();
        $response["size"]           = "0";
        $response["is_deleted"]     = true;
        $response["bytes"]          = 0;
        $response["thumb_exists"]   = MUtils::isExistThumbnail($mime_type, (int)$query_db_file[0]["file_size"]);
        $path                       = $query_db_file[0]["file_path"];
        $index                      = strlen("/{$this->_user_id}");
        $path                       = substr($path, $index);
        $response["path"]           = $path;
        $response["root"]           = $root;
        $response["is_dir"]         = $file_detail->is_dir;
        $response["rev"]            = strval($query_db_file[0]["version_id"]);
        $response["revision"]       = intval($query_db_file[0]["version_id"]);
        $response["modified"]       = MUtils::formatIntTime($query_db_file[0]["file_update_time"]);
        $response["mime_type"]      = $mime_type;
        echo json_encode($response);
    }

    
    private function handleChildrenFile($parent_path, $files)
    {
                                $db_children_files = MFiles::queryChildrenFilesByPath($parent_path, true);
        if ($db_children_files === false)
        {
            throw new MFileopsException(
                                        FXP::t('Internal Server Error'),
                                        MConst::HTTP_CODE_500);
        }
                                if (count($db_children_files) > MConst::MAX_FILES_COUNT)
        {
            throw new MFileopsException(
                                        FXP::t('Too many file'),
                                        MConst::HTTP_CODE_406);
        }
                                foreach ($db_children_files as $db_file)
        {
            $file_detail = new MFiles();
                                                if ($db_file["is_deleted"] == true)
            {
                continue;
            }
            $this->assembleFileDetail(
                                        $file_detail, 
                                        $db_file);
            array_push($files, $file_detail);
        }
                                $ret_value = MFiles::updateRemoveChildrenFile($parent_path);
        if ($ret_value === false)
        {
            throw new MFileopsException(
                                        FXP::t('Internal Server Error'),
                                        MConst::HTTP_CODE_500);
        }
        return $files;
    }
    
    
    private function handleFileMeta($file_path, $version_id, $user_id, $user_nick)
    {
                                $file_meta = MFileMetas::queryFileMeta($file_path, MConst::VERSION);
        if ($file_meta)
        {
            $meta_value = MUtils::getFileVersions($version_id, 
                                                    MConst::DELETE, 
                                                    $user_id, 
                                                    $user_nick,
                                                    $file_meta[0]["meta_value"]);
            $ret = MFileMetas::updateFileMeta(
                                        $file_meta[0]["file_path"], 
                                        MConst::VERSION, 
                                        $meta_value);
        }
        else 
        {
            $meta_value = MUtils::getFileVersions($version_id, 
                                                    MConst::DELETE, 
                                                    $user_id, 
                                                    $user_nick);
            $ret = MFileMetas::createFileMeta(
                                        $file_path, 
                                        MConst::VERSION, 
                                        $meta_value);
        }
        return $ret;
    }
    
    
    private function assembleFileDetail( $file_detail, $query_db_file )
    {
        $file_detail->event_action = MConst::DELETE;
        $file_detail->file_name    = $query_db_file["file_name"];
        $file_detail->from_path    = $query_db_file["file_path"];
        $file_detail->context      = $query_db_file["file_path"];
        $file_detail->event_uuid   = MUtils::random_string(MConst::LEN_EVENT_UUID);
    }
}
?>